home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Miscellaneous / ival / draw.c < prev    next >
Text File  |  1987-05-25  |  6KB  |  211 lines

  1. /*
  2.  * draw.c - miscellaneous drawing routines for Interval.
  3.  *
  4.  */
  5.  
  6. #include <quickdraw.h>
  7. #include <memory.h>
  8. #include <window.h>
  9. #include <resource.h>
  10. #include <toolutil.h>
  11. #include <packages.h>
  12. #include <control.h>
  13. #include <desk.h>
  14. #include <event.h>
  15. #include <font.h>
  16.  
  17. #include "ival.h"
  18. #include "progerr.h"
  19.  
  20. #define PANEFONT    systemFont    /* font for labelling panes    */
  21.  
  22. #define PANESTRS    129    /* STR# of labels for all window panes    */
  23. #define P_CNTL        1    /* index of The Control pane label    */
  24. #define P_NAME        2    /* ...The Name pane            */
  25. #define P_NOTE        3    /* ...The Notation pane            */
  26.  
  27. static FontInfo labfinfo;    /* pane label font information        */
  28.  
  29. /*
  30.  * fillwindow() - create all the data structures necessary to draw
  31.  * the program's window.
  32.  *
  33.  * Fillwindow() auto-places the rectangles of the window based on the
  34.  * window size and on the width of the names of all the buttons.
  35.  */
  36.  
  37. fillwindow()
  38. {
  39.     Rect windrect;    /* rect enclosing the window            */
  40.     short oldfont;    /* original font of this port            */
  41.     short numstrs;    /* number of strings in the current STR# resource*/
  42.     Rect udrect;    /* rect enclosing the updown[] buttons        */
  43.     Rect itrect;    /* rect enclosing the itype[] buttons        */
  44.     Rect isrect;    /* rect enclosing the isize[] buttons        */
  45.     Point delta;    /* a temporary distance                */
  46.     short width;    /* a width (in pixels)                */
  47.     Rect tmprect;    /* a temporary variable                */
  48.  
  49.     topLeft(windrect) = topLeft(windoc->portRect);
  50.     botRight(windrect) = botRight(windoc->portRect);
  51.     
  52.     oldfont = thePort->txFont;
  53.     TextFont(PANEFONT);
  54.     GetFontInfo(&labfinfo);
  55.     TextFont(oldfont);
  56.     
  57.     /*
  58.      * create the Name pane and its contents --
  59.      * its size determines the size of all other panes.
  60.      */
  61.     
  62.     fillname();
  63.      
  64.     /*
  65.      * create the other rectangles.
  66.      */
  67.     
  68.     cntlrect.left = namerect.left;
  69.     cntlrect.right = namerect.right;
  70.     cntlrect.top = windrect.top + HMARGIN +
  71.       labfinfo.descent + labfinfo.ascent + labfinfo.leading + 1;
  72.     cntlrect.bottom = namerect.top - HMARGIN;
  73.     
  74.     noterect.left = cntlrect.right + 2 * HMARGIN;
  75.     noterect.right = windrect.right - HMARGIN;
  76.     noterect.top = namerect.top;
  77.     noterect.bottom = namerect.bottom;
  78.     
  79.     /*
  80.      * Create the score structures
  81.      * then move the score to the appropriate place in its rect.
  82.      */
  83.     
  84.     getscore();
  85.     
  86.     /*
  87.      * Put the control buttons in their appropriate panes.
  88.      */
  89.  
  90.     fillcntl();
  91. }
  92.  
  93. /*
  94.  * makeradcol() - create a column of radio buttons.
  95.  * (actually, any kind of buttons you want)
  96.  */
  97.  
  98. makeradcol(buttons, procid, strid, firstrect, dh, dv)
  99. ControlHandle *buttons;        /* points to an array of buttons    */
  100. short procid;            /* button procID to use            */
  101. short strid;            /* ResID of the STR# resource to use    */
  102. Rect *firstrect;        /* where to put the first button    */
  103. short dh, dv;            /* horz/vert offset for the next button    */
  104. {
  105.     short strnum;        /* the current label string id        */
  106.     char name[64];        /* name of the current button        */
  107.     Rect butrect;        /* Rect of the current button        */
  108.     
  109.     topLeft(butrect) = topLeft(*firstrect);
  110.     botRight(butrect) = botRight(*firstrect);
  111.     
  112.     strnum = 1;
  113.     while (TRUE) {
  114.     GetIndString(name, strid, strnum);
  115.     if (name[0] == (char) 0) break;        /* Exit loop    */
  116.     buttons[strnum - 1] =
  117.       NewControl(windoc, &butrect, name,
  118.       TRUE, 0, 0, 1, procid, 0L);
  119.     
  120.     OffsetRect(&butrect, dh, dv);
  121.     ++strnum;
  122.     }
  123. }
  124.  
  125. /*
  126.  * redoc() - redraw the program's window.
  127.  *  This routine is called to satisfy update events.
  128.  */
  129.  
  130. redoc()
  131. {
  132.     GrafPtr saveport;
  133.  
  134.     BeginUpdate(windoc);
  135.     GetPort(&saveport);
  136.     SetPort(windoc);
  137.     EraseRgn(thePort->visRgn);
  138.  
  139.     panelabel(&cntlrect, P_CNTL);
  140.     panelabel(&namerect, P_NAME);
  141.     panelabel(¬erect, P_NOTE);
  142.     DrawControls(windoc);
  143.     drawscore();
  144.  
  145.     drawmouse();
  146.  
  147.     SetPort(saveport);
  148.     EndUpdate(windoc);
  149. }
  150.  
  151. /*
  152.  * panelabel() - label the given pane of the window.
  153.  * Assumes: windoc is the current port.
  154.  */
  155.  
  156. panelabel(prectp, pidx)
  157. Rect *prectp;        /* rect surrounding the pane    */
  158. short pidx;        /* string index of the label    */
  159. {
  160.     short oldfont;    /* previous font of this port        */
  161.     char label[64];    /* (pascal) text of this pane's label    */
  162.     short labwid;    /* pixel's width of that label        */
  163.     
  164.     oldfont = thePort->txFont;
  165.     TextFont(PANEFONT);
  166.     
  167.     MoveTo(prectp->left, prectp->top - 1);
  168.     LineTo(prectp->right, prectp->top - 1);
  169.  
  170.     GetIndString(label, PANESTRS, pidx);
  171.     TextFont(systemFont);
  172.     labwid = StringWidth(label);
  173.     MoveTo((prectp->left + prectp->right) / 2 - labwid / 2,
  174.       prectp->top - 1 - labfinfo.descent);
  175.     DrawString(label);
  176.     
  177.     TextFont(oldfont);
  178. }
  179.  
  180. /*
  181.  * strswidth() - return the maximum width of a set of strings.
  182.  * Also count the number of strings.
  183.  * NOTE: width is based on the current font.
  184.  */
  185.  
  186. short    /* width of the widest string */
  187. strswidth(strid, countp)
  188. short strid;        /* ResID of th STR# resource to examine        */
  189. short *countp;        /* where to put the number of strings found    */
  190. {
  191.     short strnum;    /* string index within the STR#        */
  192.     char name[64];    /* name of the current button        */
  193.     short namewidth;    /* width of the current name        */
  194.     short maxwidth;    /* width (pixels) of the widest name    */
  195.     
  196.     maxwidth = 0;
  197.     strnum = 1;
  198.     while (TRUE) {
  199.     GetIndString(name, strid, strnum);
  200.     if (name[0] == (char) 0) break;        /* Exit loop */
  201.  
  202.     namewidth = StringWidth(name);
  203.     if (maxwidth < namewidth) {
  204.         maxwidth = namewidth;
  205.     }
  206.     ++strnum;
  207.     }
  208.     *countp = strnum - 1;
  209.     return(maxwidth);
  210. }
  211.